﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.IO;

namespace WOWMultiPlay
{
    public partial class ChooseProfileForm : Form
    {
        public ChooseProfileForm()
        {
            InitializeComponent();
        }

        private void ChooseProfileForm_Load(object sender, EventArgs e)
        {
            LoadProfiles();
        }

        private void LoadProfiles()
        {
            if (!IsLoading)
            {
                IsLoading = true;

                DataTable DT = new DataTable();
                DT.Columns.Add("ProfileFileName");
                DT.Columns.Add("ProfileFilePath");

                DirectoryInfo DictInfo = new DirectoryInfo(Setting_Global.ProfilesFolderPath);
                if (!DictInfo.Exists)
                {
                    Directory.CreateDirectory(Setting_Global.ProfilesFolderPath);
                }

                FileInfo[] FilesInfo = DictInfo.GetFiles("*.ini");
                if (FilesInfo.Length == 0)
                {
                    string ProfileExampleFilePath = Path.Combine(Setting_Global.ProfilesFolderPath, "Example.ini");
                    Helper_INIFile.CreateINIFile(ProfileExampleFilePath);
                    Helper_Profile.CreateProfileExample(ProfileExampleFilePath);

                    FilesInfo = DictInfo.GetFiles("*.ini");
                }

                foreach (FileInfo FileInfo in FilesInfo)
                {
                    DataRow DR = DT.NewRow();
                    DR["ProfileFileName"] = FileInfo.Name;
                    DR["ProfileFilePath"] = FileInfo.FullName;

                    DT.Rows.Add(DR);
                }

                listBox_Profile.DataSource = DT;
                listBox_Profile.DisplayMember = "ProfileFileName";
                listBox_Profile.ValueMember = "ProfileFilePath";

                IsLoading = false;
            }
        }

        private void listBox_Profile_DoubleClick(object sender, EventArgs e)
        {
            if (listBox_Profile.SelectedItems.Count > 0)
            {
                this.DialogResult = DialogResult.OK;
                Setting_Global.Profile = Helper_Profile.LoadProfile(Convert.ToString(listBox_Profile.SelectedValue));
                Setting_Global.LoadedProfile = true;
                this.Close();
            }
        }

        bool IsLoading = false;

        private void timer_LoadProfiles_Tick(object sender, EventArgs e)
        {
            //LoadProfiles();
        }
    }
}